layui.define(["form", "layer", "larry", "laydate"], function(e) {
    "use strict";
    var i = layui.form,
        f = f = parent.layer === undefined ? layui.layer : top.layer,
        n = layui.larry,
        c = layui.laydate,
         o = layui.$,
        u = layui.$;
    var s = {
        edit: function(e, i, n, r, o) {
            var a = "";
            if (i > 0) {
                a = "修改"
            } else {
                a = "新增"
            }
            if (e == "undefined" || e == "") {
                a += "信息"
            } else {
                a += e
            }
            var t = cUrl + "/edit?id=" + i;
            if (Array.isArray(o)) {
                for (var l in o) {
                    console.log("索引：" + l + ",数据源：" + o[l]);
                    t += "&" + o[l]
                }
            }
            console.log("URL请求地址：" + t);
            s.showWin(a, t, n, r)
        },
        detail: function(e, i, n, r) {
            var o = cUrl + "/detail?id=" + i;
            s.showWin(e + "详情", o, n, r)
        },
        drop: function(n, r) {
            f.confirm("您确定要删除吗？删除后将无法恢复！", {
                icon: 3,
                skin: "layer-ext-moon",
                btn: ["确认", "取消"]
            }, function(e) {
                var i = cUrl + "/drop";
                s.ajaxPost(i, {
                    id: n
                }, function(e, i) {
                    if (r) {
                        // console.log("删除成功ID:" + e.id);
                        r(e, i)
                    }
                }, "正在删除。。。")
            })
        },
        batchDrop: function(e, i, n) {
            var r = [];
            if (e.length > 0) {
                for (var o in e) {
                    r.push(e[o].id)
                }
                var a = r.join(",");
                f.confirm("确定删除选中的数据吗？", {
                    icon: 3,
                    title: "提示信息"
                }, function(e) {
                    u.ajax({
                        url: cUrl + "/batchDrop",
                        dataType: "json",
                        type: "POST",
                        data: {
                            id: a,
                            changeAct: 0
                        },
                        beforeSend: function() {
                            f.msg("正在提交。。。", {
                                icon: 16,
                                shade: .01,
                                time: 0
                            });
                            u("#" + i).attr("disabled", "true")
                        },
                        success: function(e) {
                            u("#" + i).removeAttr("disabled");
                            f.closeAll();
                            if (e.success) {
                                f.msg(e.msg, {
                                    icon: 1,
                                    time: 1e3
                                }, function() {
                                    if (n) {
                                        n(true)
                                    }
                                })
                            } else {
                                f.msg(e.msg, {
                                    icon: 5
                                })
                            }
                        },
                        error: function() {
                            f.msg("AJAX请求异常");
                            if (n) {
                                n(false)
                            }
                        }
                    })
                })
            } else {
                f.msg("请选择需要删除的数据")
            }
        },
        verify: function() {
            i.verify({
                required: function(e, i) {
                    var n = u(i).data("title");
                    if (!n) {
                        n = u(i).parents(".layui-inline").find(".layui-form-label").text();
                        if (n.indexOf("：") >= 0) {
                            n = n.substring(0, n.Length - 1)
                        }
                    }
                    if (!e) {
                        return u(i).attr("placeholder")
                    }
                },
                number: [/^[0-9]*$/, "请输入数字"],
                username: function(e, i) {
                    if (!new RegExp("^[a-zA-Z0-9_一-龥\\s·]+$").test(e)) {
                        return title + "不能含有特殊字符"
                    }
                    if (/(^\_)|(\__)|(\_+$)/.test(e)) {
                        return title + "首尾不能出现下划线'_'"
                    }
                    if (/^\d+\d+\d$/.test(e)) {
                        return title + "不能全为数字"
                    }
                },
                pass: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"]
            })
        },
        submitForm: function(n, e=null, i=null, r=!0) {
            var u = []
                , c = []
                , s = n;
            if (o.each(s, (function(n, e) {
                    if (/\[|\]|【|】/g.test(n)) {
                        var t = n.match(/\[(.+?)\]/g);
                        e = n.match("\\[(.+?)\\]")[1];
                        var i = n.replace(t, "");
                        o.inArray(i, u) < 0 && u.push(i),
                        c[i] || (c[i] = []),
                            c[i].push(e)
                    }
                }
            )),
                o.each(u, (function(n, e) {
                        var t = [];
                        o.each(c[e], (function(n, i) {
                                t.push(i),
                                    delete s[e + "[" + i + "]"]
                            }
                        )),
                            s[e] = t.join(",")
                    }
                )),
            null == e) {
                e = cUrl;
                var f = o("form").attr("action");
                a.isEmpty(f) ? null != n.id && (0 == n.id ? e += "/add" : n.id > 0 && (e += "/update")) : e = f
            }
            l.ajaxPost(e, s, (function(n, e) {
                    if (e)
                        return r && setTimeout((function() {
                                t.closeAll("iframe"),
                                    parent.location.reload()
                            }
                        ), 100),
                        i && i(n, e),
                            !1
                }
            ))
        },
        searchForm: function(e, i, n) {
            if (!n) {
                n = "tableList"
            }
            e.reload(n, {
                page: {
                    curr: 1
                },
                where: i.field
            })
        },
        initDate: function(e, r) {
            if (Array.isArray(e)) {
                for (var i in e) {
                    console.log("索引：" + i);
                    console.log("组件元素：" + e[i]);
                    var n = e[i].split("|");
                    console.log("组件ID：" + n[0]);
                    if (n[2]) {
                        console.log("日期参数：" + n[2]);
                        var o = n[2].split(",")
                    }
                    var a = {};
                    a.elem = "#" + n[0];
                    a.type = n[1];
                    a.theme = "molv";
                    a.range = n[3] === "true" ? true : n[3];
                    a.calendar = true;
                    a.show = false;
                    a.position = "absolute";
                    a.trigger = "click";
                    a.btns = ["clear", "now", "confirm"];
                    a.mark = {
                        "0-06-25": "生日",
                        "0-12-31": "跨年"
                    };
                    a.ready = function(e) {
                        console.log("组件面板打开：" + e)
                    };
                    a.change = function(e, i, n) {
                        console.log(e);
                        console.log(i);
                        console.log(n)
                    };
                    a.done = function(e, i, n) {
                        if (r) {
                            r(e, i)
                        }
                    };
                    if (o) {
                        var t = o[0];
                        if (t) {
                            var l = !isNaN(t);
                            if (l) {
                                a.min = parseInt(t)
                            } else {
                                a.min = t
                            }
                        }
                        var s = o[1];
                        if (s) {
                            var f = !isNaN(s);
                            if (f) {
                                a.max = parseInt(s)
                            } else {
                                a.max = s
                            }
                        }
                    }
                    c.render(a)
                }
            }
        },
        showWin: function(e, i, n, r, a, o, t, l) {
            if (!n) n = 0;
            if (!r) r = 0;
            if (!o) o = 2;
            var s = layui.layer.open({
                title: e,
                type: o,
                area: [n + "px", r + "px"],
                content: i,
                shadeClose: true,
                shade: .4,
                maxmin: true,
                btn: t,
                btnAlign: "c",
                success: function(e, i) {
                    if (Array.isArray(a)) {
                        for (var n in a) {
                            var r = a[n].split("=");
                            console.log("传值：" + r[0] + "," + r[1]);
                            var o = layui.layer.getChildFrame("body", i);
                            o.find("#" + r[0]).val(r[1])
                        }
                    }
                    if (l) {
                        l(e, i)
                    }
                    setTimeout(function() {
                        f.tips("点击此处返回列表", ".layui-layer-setwin .layui-layer-close", {
                            tips: 3
                        })
                    }, 500)
                },
                end: function() {}
            });
            if (n == 0) {
                layui.layer.full(s);
                u(window).on("resize", function() {
                    layui.layer.full(s)
                })
            }
        },
        ajaxPost: function(e, i, n, r) {
            if (!r) {
                r = "处理中,请稍后。。。"
            }
            var o = "";
            u.ajax({
                url: e,
                dataType: "json",
                type: "POST",
                data: i,
                beforeSend: function() {
                    o = f.msg(r, {
                        icon: 16,
                        shade: .01,
                        time: 0
                    })
                },
                success: function(e) {
                    if (e.success) {
                        f.msg(e.msg, {
                            icon: 1,
                            time: 500
                        }, function() {
                            f.close(o);
                            if (n) {
                                n(i, true)
                            }
                        })
                    } else {
                        f.msg(e.msg, {
                            icon: 5
                        });
                        return false
                    }
                },
                error: function() {
                    f.msg("AJAX请求异常");
                    if (n) {
                        n(i, false)
                    }
                }
            })
        }
    };
    e("common", s)
});